package com.shu.project.service.impl;

import com.shu.framework.enums.Chanel;
import com.shu.framework.enums.OrderStatusEnum;
import com.shu.project.dao.OrderDao;
import com.shu.project.dao.SeckillOrderDao;
import com.shu.project.model.Order;
import com.shu.project.model.SeckillOrder;
import com.shu.project.model.User;
import com.shu.project.service.IOrderService;
import com.shu.project.vo.GoodsVo;
import com.shu.project.vo.OrderVo;
import com.shu.project.vo.SeckillOrderVo;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
import java.util.Map;


@Service("orderService")
public class OrderServiceImpl implements IOrderService {
    private static final Logger log = LoggerFactory.getLogger(OrderServiceImpl.class);

    @Autowired
    SeckillOrderDao seckillOrderDao;
    @Autowired
    OrderDao orderDao;
    @Override
    public List<SeckillOrderVo> queryOrderByUserIdGoodsId(Map<String,Long> map) {
        return seckillOrderDao.queryOrderByUserIdGoodsId(map);
    }

    @Override
    public Order createOrder(User user, GoodsVo goodsVo) {
        Order order = new Order();
        order.setCreateTime(new Date());
        order.setDeliveryAddrId(0l);
        order.setGoodsId(goodsVo.getId());
        order.setUserId(user.getId());
        order.setGoodsName(goodsVo.getGoodsName());
        order.setGoodsCount(1);
        order.setGoodsPrice(goodsVo.getSeckillPrice());
        order.setUpdateTime(new Date());
        order.setOrderChanel(Chanel.PC.getChanel());
        order.setStatus(OrderStatusEnum.未支付.getStatus());
        int insert = orderDao.insert(order);
        log.info("创建订单成功{}",insert);
        SeckillOrder seckillOrder = new SeckillOrder();
        seckillOrder.setGoodsId(order.getGoodsId());
        seckillOrder.setOrderId(order.getId());
        seckillOrder.setUserId(order.getUserId());
        insert = seckillOrderDao.insert(seckillOrder);
        log.info("创建秒杀订单成功{}",insert);
        return order;
    }
}
